//
//  UserViewController.m
//  豆瓣
//
//  Created by dllo on 15/12/7.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "UserViewController.h"
#import "LoginViewController.h"
#import "UserTableViewCell.h"
#import "MyMovieViewController.h"
#import "Movie.h"
#import "MyMovieViewController.h"
#import "SDImageCache.h"
#import "MyActivityViewController.h"


@interface UserViewController ()<UITableViewDataSource, UITableViewDelegate, LoginViewControllerDelegate>

@property (nonatomic, retain) UITableView *myTableView;
@property (nonatomic, retain) LoginViewController *loginVC;

@end

@implementation UserViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"我的";
    
    self.myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    [self.myTableView registerClass:[UserTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_myTableView];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [_myTableView release];
    
    

//    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"BOOL"] isEqualToString:@"no"]) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"登陆" style:UIBarButtonItemStylePlain target:self action:@selector(buttonAction:)];
        self.navigationItem.rightBarButtonItem.tintColor = [UIColor blackColor];
//    }
//    if([[[NSUserDefaults standardUserDefaults] objectForKey:@"BOOL"] isEqualToString:@"yes"]){
//        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注销" style:UIBarButtonItemStylePlain target:self action:@selector(buttonAction:)];
//        self.navigationItem.rightBarButtonItem.tintColor = [UIColor blackColor];
//
//    }

    
    self.loginVC.myDelegate = self;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    NSString *accout = [[NSUserDefaults standardUserDefaults] objectForKey:@"account"];
//    NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
    NSLog(@"%@", [[NSUserDefaults standardUserDefaults] objectForKey:@"BOOL"]);
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"BOOL"] isEqualToString:@"no"]) {
              self.navigationItem.rightBarButtonItem.title = @"登陆";
        
    }else if([[[NSUserDefaults standardUserDefaults] objectForKey:@"BOOL"] isEqualToString:@"yes"]){
        self.navigationItem.rightBarButtonItem.title = @"注销";
        
    }
}
- (void)changeRightButton{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注销" style:UIBarButtonItemStylePlain target:self action:@selector(logoffButtonAction:)];
}

//- (void)logoffButtonAction:(UIBarButtonItem *)button{
//    
//      self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"登陆" style:UIBarButtonItemStylePlain target:self action:@selector(buttonAction:)];
//    
//}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (indexPath.row == 0) {
        cell.title.text = @"我的活动";
    }
    if (indexPath.row == 1) {
        cell.title.text = @"我的电影";
    }
    if (indexPath.row == 2) {
        cell.title.text = @"清除缓存";
    }
    
    
    return cell;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

- (void)buttonAction:(UIBarButtonItem *)btn{
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"BOOL"] isEqualToString:@"no"]) {
    self.loginVC = [[LoginViewController alloc] init];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:_loginVC];
//    [self.navigationController pushViewController:loginVC animated:YES];
//        self.navigationItem.rightBarButtonItem.title = @"登陆";

    [self presentViewController:naVC animated:YES completion:^{
     
     
     
    }];
    }
    if([[[NSUserDefaults standardUserDefaults] objectForKey:@"BOOL"] isEqualToString:@"yes"]){
        
        [[NSUserDefaults standardUserDefaults] setObject:@"no" forKey:@"BOOL"];
//        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"account"];
//        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"password"];
        self.navigationItem.rightBarButtonItem.title = @"登陆";
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"注销成功" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *alertAc = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }];
        [alert addAction:alertAc];
        [self presentViewController:alert animated:YES completion:^{
            
            
        }];
    }
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        MyActivityViewController *myActVC = [[MyActivityViewController alloc] init];
        [self.navigationController pushViewController:myActVC animated:YES];
    }
    if (indexPath.row == 1) {
        MyMovieViewController *myMovieVC = [[MyMovieViewController alloc] init];
        [self.navigationController pushViewController:myMovieVC animated:YES];
    }
    if (indexPath.row == 2) {
        NSInteger size = [[SDImageCache sharedImageCache] getSize];
        float sizeMB = size / 1024.0 / 1024.0;
        NSLog(@"大小为 %.2f", sizeMB);
        // 清除图片缓存, 使用第三方中的方法
        [[SDImageCache sharedImageCache] clearDisk];
        NSString *str = [NSString stringWithFormat:@"已清除%.2fMB缓存", sizeMB];
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *alAC = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:alAC];
        [self presentViewController:alert animated:YES completion:^{
            
            
        }];
        
        
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
